<?php
/*	sslib@hazg.net	*/	

define('SSLIB_Session', TRUE);

ssinc('user');

/**
* 
*/
class Session
{
	private static $COOKIENAME 		= 'sslibsession';
	private static $COOK_ID 		= 0;
	private static $COOK_SECUR 		= 1;
	
	static function Begin()
	{
		session_start();
		if(isset($_SESSION,Session::$COOKIENAME))
		{
			return true;
		}
		if($cookies = Session::getCookie())
		{
			if(Session::checkSession($cookies))
			{
				Session::storeSession($cookies[Session::$COOK_ID], $cookies[Session::$COOK_SECUR]);
			}
			else 
			{
				//die('hack attempt.');
				Session::Logout();
				header('Location: /');
			}
		}
		return false;
	}
	/**
	 * Return current user information
	 *
	 * @return User
	 */
	static function User()
	{
		//if($_SESSION['sslibuserinfo'] )
		return new User(Session::UserID());
	}
	
	static function UserID($login = false)
	{
		if(!$login)
		{
			if(isset($_SESSION[Session::$COOKIENAME])) return $_SESSION[Session::$COOKIENAME];
			if($cookies = Session::getCookie())
			{
				
				return $cookies[Session::$COOK_ID];
			}
		}
		else 
		{
			
			$login = Sql::QuoteValue($login);
			$db = Sslib::DB();
			$data = $db->Query('SELECT id FROM '.Sslib::TableUsers()." WHERE nick=$login LIMIT 1");
			if($data->IsEmpty()) return false;
			$row = $data->Row();
			return $row['id'];
		}
		return false;
	}
	static function Logout()
	{
		Session::clearClientCookies();
		session_unregister(Session::$COOKIENAME);
	}
	static function Check($login, $password)
	{
		$id = Session::UserID($login);
		if(!$id) return false;
		return Session::checkEncrypted($id, Session::encrypt($password));
	}
	
	static function Login($login, $password)
	{
		Session::clearClientCookies();
		Session::storeSession(Session::UserID($login),  Session::encrypt($password));
	}
	static function Remove($key)
	{
	    if(array_key_exists($key, $_SESSION))
	        unset($_SESSION[$key]);
	}
	static function Get($key)
	{
	    if(array_key_exists($key, $_SESSION))
	        return $_SESSION[$key];
	    return false;
	}
	static function Set($key, $val)
	{
	    $_SESSION[$key] = $val;
	}
	
	private static function storeSession($id, $encpass)
	{
		$arr = array();
		$arr[Session::$COOK_ID] = $id;
		$arr[Session::$COOK_SECUR] = $encpass;
		$_SESSION[Session::$COOKIENAME] = $id;
		Session::setCookie(Session::$COOKIENAME, implode('/',$arr));
	}
	private static function setCookie($name, $val){
		setcookie($name, $val, time()+60*60*24*30, "/", '.'.Request::Host(true), 0);
	}
	private static function getCookie()
	{
		
		if(array_key_exists(Session::$COOKIENAME, $_COOKIE))
		{
			$arr = explode('/',$_COOKIE[Session::$COOKIENAME]);
			if(sizeof($arr) < 2){ clearCLientCookies(); return false;}
			if($arr[Session::$COOK_ID] < 1) return false;
			if(empty($arr[Session::$COOK_SECUR])) return false;
			return $arr;
		}
		return false;
	}
	private static function encrypt($text)	{return md5($text);							}
	private static function clearClientCookies()	{setcookie(Session::$COOKIENAME, '', -1, '/', '.'.Request::Host(true));	}
	
	private static function checkSession($idSlashEncrypted)
	{
		$arr = explode('/',$idSlashEncrypted);
		if(sizeof($arr) == 2)
		{
			return checkEncrypted($arr[Session::$COOK_ID], $arr[Session::$COOK_SECUR]);
		}
		return false;
	}
	private static function checkEncrypted($id, $password)
	{
		$data = Q('SELECT pass FROM '.Sslib::TableUsers().' WHERE id='.qV($id).' LIMIT 1');
		if($data->IsEmpty()) return false;
		$row = $data->Row();
		if( Session::encrypt($row['pass']) == $password){return true;}
		
		return false;
	}
	
}
?>